package com.yinhe.music.yhmusic.adapter;

import android.graphics.Color;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yinhe.music.common.utils.KeywordUtil;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.db.DataBaseAccessor;
import com.yinhe.music.yhmusic.downmusic.DownloadStatus;
import com.yinhe.music.yhmusic.model.Music;


/**
 * @author hasee
 * @date 2017/8/15
 */

public class MusicListAdapter extends BaseQuickAdapter<Music, BaseViewHolder> {
    private String key;
    private Music.Type type;

    public MusicListAdapter(String key) {
        super(R.layout.new_music_recyclerview_item);
        this.key = key;
    }

    public MusicListAdapter(Music.Type type) {
        super(R.layout.new_music_recyclerview_item);
        this.type = type;
    }

    public void setSearchKey(String key) {
        this.key = key;
    }

    @Override
    protected void convert(BaseViewHolder helper, Music item) {
        String songName = item.getSongName();
        String singerName = item.getSingerName();
        helper.setText(R.id.playlist_song, item.getSongName())
                .setText(R.id.playlist_singer, item.getSingerName());
        helper.addOnClickListener(R.id.playlist_more);

        if (key != null) {
            helper.setText(R.id.playlist_song,
                    KeywordUtil.matcherSearchTitle(Color.parseColor("#1d89ff"), songName, key));
            helper.setText(R.id.playlist_singer,
                    KeywordUtil.matcherSearchTitle(Color.parseColor("#1d89ff"), singerName, key));
        }

        if (DataBaseAccessor.getInstance().isExsitIdSyn(item.getSongId(),
                DownloadStatus.DOWNLOAD_STATUS_COMPLETED)) {
            helper.setVisible(R.id.image_get, true);
        }

        if (DataBaseAccessor.getInstance().isExsitIdSyn(item.getSongId(),
                DownloadStatus.DOWNLOAD_STATUS_DOWNLOADING)) {
            helper.setVisible(R.id.down_progress, true);
        }

        if (item.getMovieId() > 0) {
            helper.setVisible(R.id.playlist_chat_movie, true)
                    .addOnClickListener(R.id.playlist_chat_movie);
        } else {
            helper.setVisible(R.id.playlist_chat_movie, false);
        }

        if (type != null && type == Music.Type.LOCAL) {
            if (item.getNationalType() == 0) {
                helper.setVisible(R.id.image_lang, true);
                helper.setImageResource(R.id.image_lang, R.drawable.ic_zang);
            } else if (item.getNationalType() == 1) {
                helper.setVisible(R.id.image_lang, true);
                helper.setImageResource(R.id.image_lang, R.drawable.ic_huaer);
            } else {
                helper.setVisible(R.id.image_lang, false);
            }
        }

        if (DataBaseAccessor.getInstance().isExistSongDBId((int) item.songId)) {
            helper.setVisible(R.id.image_local, true);
        }
    }
}
